package com.thilux.lame.logging.log4j;

import org.apache.log4j.Logger;

/**
 * <p>
 * This is an utility class for logging using Log4j. It simplifies and keeps
 * out the need of having an instance of the Logger class in each of the classes
 * that needs logging. One limitation so far, is that for printing the name of
 * the calling method.
 * </p>
 * 
 * @author tsantana
 * 
 */
public class LameLogger {

	private static Logger logger = Logger.getRootLogger();

	/**
	 * <p>
	 * This is a tweak method to identify the name of the calling class so that
	 * the logger is not caught and stuck with the name of the LameLogger only.
	 * </p>
	 */
	private static void resetLogger() {

		StackTraceElement[] stackTraceElements = Thread.currentThread()
				.getStackTrace();

		StackTraceElement lastStackTraceElement = stackTraceElements[3];
		logger = Logger.getLogger(lastStackTraceElement.getClassName());

	}

	/**
	 * A method used to log a message at the DEBUG level.
	 * 
	 * @param message
	 *            - The message to be logged.
	 */
	public static void logDebug(String message) {

		resetLogger();
		logger.debug(message);

	}

	/**
	 * A method used to log a message at the DEBUG level.
	 * 
	 * @param message
	 *            - The message to be logged.
	 * @param parameters
	 *            - A list of parameters to be replaced in the string as
	 *            formatters.
	 */
	public static void logDebug(String message, Object... parameters) {

		resetLogger();
		logger.debug(String.format(message, parameters));

	}

	/**
	 * A method used to log a message at the INFO level.
	 * 
	 * @param message
	 *            - The message to be logged.
	 */
	public static void logInfo(String message) {

		resetLogger();
		logger.info(message);

	}

	/**
	 * A method used to log a message at the INFO level.
	 * 
	 * @param message
	 *            - The message to be logged.
	 * @param parameters
	 *            - A list of parameters to be replaced in the string as
	 *            formatters.
	 */
	public static void logInfo(String message, Object... parameters) {

		resetLogger();
		logger.info(String.format(message, parameters));

	}

	/**
	 * A method used to log a message at the TRACE level.
	 * 
	 * @param message
	 *            - The message to be logged.
	 */
	public static void logTrace(String message) {

		resetLogger();
		logger.trace(message);

	}

	/**
	 * A method used to log a message at the TRACE level.
	 * 
	 * @param message
	 *            - The message to be logged.
	 * @param parameters
	 *            - A list of parameters to be replaced in the string as
	 *            formatters.
	 */
	public static void logTrace(String message, Object... parameters) {

		resetLogger();
		logger.trace(String.format(message, parameters));

	}

	/**
	 * A method used to log a message at the WARNING level.
	 * 
	 * @param message
	 *            - The message to be logged.
	 */
	public static void logWarn(String message) {

		resetLogger();
		logger.warn(message);

	}

	/**
	 * A method used to log a message at the WARNING level.
	 * 
	 * @param message
	 *            - The message to be logged.
	 * @param parameters
	 *            - A list of parameters to be replaced in the string as
	 *            formatters.
	 */
	public static void logWarn(String message, Object... parameters) {

		resetLogger();
		logger.warn(String.format(message, parameters));

	}

	/**
	 * A method used to log a message at the ERROR level.
	 * 
	 * @param message
	 *            - The message to be logged.
	 */
	public static void logError(String message) {

		resetLogger();
		logger.error(message);

	}

	/**
	 * A method used to log a message at the ERROR level.
	 * 
	 * @param message
	 *            - The message to be logged.
	 * @param parameters
	 *            - A list of parameters to be replaced in the string as
	 *            formatters.
	 */
	public static void logError(String message, Object... parameters) {

		resetLogger();
		logger.error(String.format(message, parameters));

	}

	/**
	 * A method used to log a message at the FATAL level.
	 * 
	 * @param message
	 *            - The message to be logged.
	 */
	public static void logPanic(String message) {

		resetLogger();
		logger.fatal(message);

	}

	/**
	 * A method used to log a message at the FATAL level.
	 * 
	 * @param message
	 *            - The message to be logged.
	 * @param parameters
	 *            - A list of parameters to be replaced in the string as
	 *            formatters.
	 */
	public static void logPanic(String message, Object... parameters) {

		resetLogger();
		logger.fatal(String.format(message, parameters));

	}

}
